import { XMLParser, XMLBuilder } from 'fast-xml-parser'

const parser = new XMLParser({
    parseTagValue: false,
    transformTagName: (tag) => tag.toLowerCase(),
})

const keepTagParser = new XMLParser({
    parseTagValue: false,
})

export function parseXml<T extends object = Record<string, any>>(xml: string, transformTagName = true): T {
    return transformTagName ? parser.parse(xml).xml : keepTagParser.parse(xml).xml
}

const builder = new XMLBuilder()
export function buildXml(data: Record<string, any>): string {
    return builder.build({ xml: data })
}
